package com.example.chess.utils;

import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;

public class Utils {

    public static int dp2px(float dp) {
        return (int) (Resources.getSystem().getDisplayMetrics().density * dp);
    }

    public static int getStatusBarHeight() {
        Resources resources = Resources.getSystem();
        int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelOffset(identifier);
    }

    public static void fitWithStatusBar(View view) {
        view.post(() -> {
            ViewGroup.LayoutParams params = view.getLayoutParams();
            int barHeight = getStatusBarHeight();
            int paddingTop = view.getPaddingTop() + barHeight;
            if (params.height != ViewGroup.LayoutParams.WRAP_CONTENT) {
                params.height += barHeight;
            }
            view.setPadding(view.getPaddingLeft(), paddingTop, view.getPaddingRight(), view.getPaddingBottom());
            view.requestLayout();
        });
    }

}
